/**
 * SearchPropertyUtils.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年3月15日
 */
package com.pshare.util;

import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

import com.pshare.constant.SeaxConstants;

/**
 * 搜索配置项读取工具类
 */
public class SearchPropertyUtils {

	private static Properties props = null;
	private static String PROPERTIES_FILE_LOCATION = null;

	private static PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();
	
	protected static final Logger logger = Logger.getLogger(SearchPropertyUtils.class);

	public static void setPropLocation(String location) {
		PROPERTIES_FILE_LOCATION = location.substring(location.lastIndexOf(SeaxConstants.SLASH) + 1);
		props = PropertiesUtils.loadProperties(location);
	}

	public static String getProperty(String key) {
		String propertyValue = "";
		if (StringUtils.isBlank(key)) {
			return propertyValue;
		}

		// 如果加载上下文的时候没有读取成功
		if (props == null && PROPERTIES_FILE_LOCATION != null) {
			props = PropertiesUtils.loadProperties(PROPERTIES_FILE_LOCATION);
			// 如果仍然没有读取到
			if (props == null) {
				logger.error("======项目启动异常，请检查配置文件路径======");
			}
		}

		if (key.indexOf(SeaxConstants.PLUS) > -1) {
			String[] keys = key.split(SeaxConstants.BLACKSLASH + SeaxConstants.PLUS);
			if (keys.length < 1) {
				return propertyValue;
			}
			for (String k : keys) {
				propertyValue += props.getProperty(k.trim());
			}
		} else {
			propertyValue = props.getProperty(key.trim());
		}
		
		return propertyValue;
	}
	
}
